<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="键位名" prop="bName">
        <el-input v-model="queryParams.bName" placeholder="请输入键位名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="履约表字段值" prop="bValue">
        <el-input v-model="queryParams.bValue" placeholder="请输入履约表字段值" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否选择 0 否 1 是" prop="bSfxz">
        <el-input v-model="queryParams.bSfxz" placeholder="请输入是否选择 0 否 1 是" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="排序" prop="bOrder">
        <el-input v-model="queryParams.bOrder" placeholder="请输入排序" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-edit-outline" size="mini" @click="updatePositionFun">保存</el-button>
      </el-col>
    </el-row>

    <el-table row-key="id" ref="elTable" v-loading="loading" :data="positionList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="键位名" align="center" prop="bName" />
      <el-table-column label="是否选择" align="center">
        <template slot-scope="scope">
          <span>{{ statusDict[scope.row.bSfxz] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="bOrder" />
    </el-table>
  </div>
</template>

<script>
import { listPosition, getPosition, delPosition, addPosition, updatePosition } from "@/api/guarantee/position";
import Sortable from "sortablejs";

export default {
  name: "Position",
  data() {
    return {
      statusDict: {
        '0': '否',
        '1': '是',
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 履约一键导出表格数据
      positionList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bName: null,
        bValue: null,
        status: null,
        bSfxz: null,
        bOrder: null
      },
    };
  },
  created() {
    this.getList();
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    }
  },
  mounted() {
    this.rowDrop();
  },
  methods: {
    /** 回显选中table */
    checkedKeys() {
      this.$nextTick(() => {
        const list = this.positionList.filter((item) => item.bSfxz === 1)
        if (list.length != 0) {
          list.forEach((row) => {
            this.$refs.elTable.toggleRowSelection(row, true)
          })
        }
      });
    },
    /** 行拖拽 */
    rowDrop() {
      const tbody = document.querySelector(".el-table__body-wrapper tbody");
      const that = this;
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          const currRow = that.positionList.splice(oldIndex, 1)[0];
          that.positionList.splice(newIndex, 0, currRow);
          that.positionList.forEach((item, index) => {
            item.bOrder = index + 1
          })
        },
      });
    },
    /** 查询履约一键导出列表 */
    getList() {
      this.loading = true;
      listPosition(this.queryParams).then(response => {
        this.positionList = response.data;
        this.checkedKeys()
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      const ids = selection.map((item) => item.id)
      this.positionList.forEach((ite, i) => {
        if (ids.includes(ite.id)) {
          ite.bSfxz = '1'
        } else {
          ite.bSfxz = '0'
        }
      })
    },
    // 保存数据
    updatePositionFun(){
      updatePosition(this.positionList).then(res => {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
      });
    }
  }
};
</script>
